// h('div','hello')
// h('div',{style:'color:red;'})
// h('div',{style:'color:red;'},'hello')
// h('div',{style:'color:red;'},h('div'))
// h('div',{style:'color:red;'},[h('div')])

import { isArray, isObject, isString } from '../share'
import { vnode } from './vnode'

export function h(tag, props, children) {
  if (isString(props)) {
    children = props
    props = {}
  }
  if (!isObject(props)) {
    props = {}
  }
  let text = null
  if (isObject(children) && !isArray(children)) {
    children = [children]
  } else if (isObject(children) && isArray(children)) {
    children = children.map(child => {
      // 文本节点
      if (!child.tag && isString(child)) {
        return vnode(null, null, null, null, child, [])
      }
      // 表达式文本
      if (!child.tag && isObject(child)) {
        return vnode(null, null, null, child, null, [])
      }
      return h(child.tag, child.props, child.children)
    })
  }
  return vnode(tag, props.key, null, props, text, children)
}
